<?php

namespace app\console\model;

use think\Model;
use think\Request;
use app\common\trilateral\Classify as ClassifyAlias;
use think\Session;
class GoodsType extends Model
{

    // 模型事件
    protected static function init()
    {
        // 写入前
        self::event('before_write', function ($data) {
            $data->picUrl = uploadImg('picUrl', 'shop');
//            $data->ad_img = uploadImg('ad_img', 'shop');
            $data->brand = self::Brand();
            $data->parentStr = parentStr(Request::instance()->param('parenTid'), 'goods_type', Request::instance()->param('id'), Request::instance()->param('repId'));
            $data->count = self::CountAttr();
            auding($data);
        });
    }

    // 牌品方法
    protected static function Brand()
    {
        $brand = Request::instance()->param('brand/a');

        if (!empty($brand)) {

            return implode(',', $brand);

        }

        return false;
    }

    public function getAudingTextAttr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败'];
        return $status[$data['auding_status']];
    }

    public function getAudingText1Attr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败'];
        return $status[$data['auding_status1']];
    }
    public function getAudingText2Attr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败'];
        return $status[$data['auding_status2']];
    }
    public function getAudingText3Attr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败'];
        return $status[$data['auding_status3']];
    }

    // count
    protected static function CountAttr()
    {

        if (Request::instance()->param('id'))
            $count = ClassifyAlias::countUpdate(Request::instance()->param('id'), Request::instance()->param('parenTid'), 'goods_type');
        else
            $count = ClassifyAlias::countAdd(Request::instance()->param('parenTid'), 'goods_type');

        // 判断是否大于3级
        if ($count > '3') {

            fail('最大只能是三级分类');

        }

        return $count;
    }

    // 删除方法
    public static function amputate($id)
    {

        if (self::where('parentStr', 'like', '%,' . $id . ',%')->whereOr('id', $id)->delete()) {
            return true;
        }

        return false;

    }

    // 全部删除方法
    public static function amputateAll($id)
    {
       
        foreach ($id as $value) {
            self::where('parentStr', 'like', '%,' . $value . ',%')->whereOr('id', $value)->delete();
        }

        return true;

    }

}
